﻿namespace ReorganizationSeries
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Globalization;

    public static class Reorganization
    {
        public static void FromNumValueToValueWithCount(
            String source,
            String dest)
        {
            Stream inpStream = File.Open(source, FileMode.Open);
            Stream outStream = File.Open(dest, FileMode.Create);

            StreamReader sr = new StreamReader(inpStream);
            StreamWriter sw = new StreamWriter(outStream);

            String str;
            Int32 length;

            CultureInfo culture = null;
            culture = (CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
            System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            try
            {
                str = sr.ReadLine();
                Int32.TryParse(str, out length);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            sw.WriteLine(length);
            
            try
            {
                int num;
                double item;
                String[] res;
                for (int i = 0; i < length; i++)
                {
                    res = sr.ReadLine().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    Int32.TryParse(res[0], out num);
                    
                    item = Convert.ToDouble(res[1]);
                    sw.WriteLine(item);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            sr.Close();
            sr.Dispose();
            sw.Close();
            sw.Dispose();
            inpStream.Close();
            inpStream.Dispose();
            outStream.Close();
            outStream.Dispose();

            System.Threading.Thread.CurrentThread.CurrentCulture = (CultureInfo)culture.Clone();
            culture = null;
        }

        public static void FromNumValueToValueWithOutCount(
            String source,
            String dest)
        {
            Stream inpStream = File.Open(source, FileMode.Open);
            Stream outStream = File.Open(dest, FileMode.Create);

            StreamReader sr = new StreamReader(inpStream);
            StreamWriter sw = new StreamWriter(outStream);

            List<double> array = new List<double>();

            CultureInfo culture = null;
            culture = (CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
            System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            
            try
            {
                int num;
                double item;
                String[] res;
                while(!sr.EndOfStream)
                {
                    res = sr.ReadLine().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    Int32.TryParse(res[0], out num);

                    item = Convert.ToDouble(res[1]);
                    array.Add(item);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            sw.WriteLine(array.Count);
            for (int i = 0; i < array.Count; i++ )
            {
                sw.WriteLine(array[i]);
            }

            sr.Close();
            sr.Dispose();
            sw.Close();
            sw.Dispose();
            inpStream.Close();
            inpStream.Dispose();
            outStream.Close();
            outStream.Dispose();

            System.Threading.Thread.CurrentThread.CurrentCulture = (CultureInfo)culture.Clone();
            culture = null;
        }
    }
}
